package com.xbongbong.paas.service.workflow.setting.impl;

import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.workflow.settings.WorkflowGetPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.WorkflowNodeUtil;
import com.xbongbong.pro.enums.workflow.WorkflowValueTypeEnum;
import com.xbongbong.workflow.pojo.nodes.WorkflowConditionPojo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/01 20:05
 */
@Service("conditionNodeLabelServiceImpl")
public class ConditionNodeLabelServiceImpl extends AbstractConditionNodeFieldService {



    @Override
    public List<Integer> type() {
        return Arrays.asList(FieldTypeEnum.LABEL.getType());
    }


    @Override
    public String conditionField(WorkflowConditionPojo workflowConditionPojo, WorkflowGetPojo workflowGetPojo) throws XbbException {
        String value = "";
        Map<String, FieldAttrEntity> explainMap = WorkflowNodeUtil.getExplainMap(workflowGetPojo, workflowConditionPojo.getBusinessType(), workflowConditionPojo.getFormId());
        FieldAttrEntity fieldAttrEntity = explainMap.get(workflowConditionPojo.getAttr());
        String symbol = ConditionEnum.getMemoBySymbo(workflowConditionPojo.getSymbol());
        String attrName = fieldAttrEntity.getAttrName();
        if (Objects.equals(WorkflowValueTypeEnum.FIXED_VALUE.getType(), workflowConditionPojo.getValueType())) {
            if (CollectionUtils.isNotEmpty(workflowConditionPojo.getValue())) {
                List<Object> valueList = new ArrayList<>();
                Map<Long, String> labelMap = workflowGetPojo.getLabelMap();
                workflowConditionPojo.getValue().forEach(item->{
                    valueList.add(labelMap.get(Long.valueOf(item.toString())));
                });
                value = StringUtils.join(valueList, "、");
            }else {
                // 为空、不为空
                value = attrName + symbol;
            }
        } else {
            value = getDynamicValue(attrName, symbol, workflowGetPojo, workflowConditionPojo);
        }
        return value;
    }
}
